// reverse
var reversePrint = function (head) {
  if (head == null) return [];
  let res = [];
  while (head) {
    res.push(head.val);
    head = head.next;
  }
  return res.reverse();
};

// 反转链表
var reversePrint = function (s) {
  if (head == null && head.next == null) return head;
  let p = head.next;
  head.next = null;
  let temp = null;
  while (p != null) {
    temp = p.next; // temp 指针前进（保存下一个指针信息）
    p.next = head; // 指针反转
    head = p; // head 指针前进
    p = temp; // p 指针前进
  }
  return head;
};

// 添加 prev 属性引用前一个
var reversePrint = function (head) {
  const res = [];
  // 因为这个判断条件很巧妙，执行完 head.next 刚好就是 head
  while (head && head.next) {
    head.next.prev = head;
    head = head.next;
  }
  while (head) {
    res[res.length] = head.val;
    head = head.prev;
  }
  return res;
};
